package com.gxa.autoparts.admin.controller;


import com.gxa.autoparts.admin.service.*;
import com.gxa.autoparts.admin.vo.AdminRoleVo;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.Admin;
import com.gxa.autoparts.core.pojo.AdminRole;
import com.gxa.autoparts.core.pojo.Role;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/accountManage")
@RestController
@Transactional(rollbackFor = Throwable.class)
public class AccountManageController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private LogService logService;

    @ApiOperation("账号管理查询全部")
    @PostMapping("/findAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "页内数据条数", required = true)
    })
    public Map findAllAccount(Integer page,Integer size){

        List<Map> maps = adminService.selectAllAdmin((page - 1)*size, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.setMsg("管理员全查成功");
        jsonResult.put("totleNumber",adminService.selectAllAdmin2().size());
        return jsonResult.getDataMap();
    }

    @ApiOperation("更新管理员启用状态")
    @PostMapping("/updateManagerState")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "管理员Id", required = true),
            @ApiImplicitParam(name = "state", value = "账号状态", required = true)
    })
    public Map updateManagerState(Long id,Boolean state) throws BusinessException, UnknownHostException {

        Admin admin = adminService.selectById(id);
        if (state==admin.getState()){
            throw new BusinessException(ExceptionCode.WRONG_ADMIN_STATE);
        }
        admin.setState(state);
        adminService.updateById(admin);
        logService.insertLog("更新管理员启用状态",admin.getName());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("更新管理员启用状态成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private AdminRoleService adminRoleService;

    @ApiOperation("删除管理员")
    @PostMapping("/deleteManager")
    @ApiImplicitParam(name = "id", value = "管理员Id", required = true)
    public Map deleteManager(Long id) throws UnknownHostException {
        adminService.deleteById(id);
        AdminRole adminRole = new AdminRole();
        adminRole.setAid(id);
        List<AdminRole> select = adminRoleService.select(adminRole);
        for (AdminRole adminRole1:select){
            adminRoleService.deleteById(adminRole1.getId());
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除管理员成功");
        return jsonResult.getDataMap();
    }
    @Autowired
    private RoleService roleService;
    @ApiOperation("根据Id查询管理员")
    @PostMapping("/selectManagerById")
    @ApiImplicitParam(name = "id", value = "管理员Id", required = true)
    public Map selectManagerById(Long id) throws BusinessException, UnknownHostException {
        Admin admin = adminService.selectById(id);
        AdminRole adminRole = new AdminRole();
        adminRole.setAid(id);
        List<AdminRole> select = adminRoleService.select(adminRole);
        if (select.size()!=1){
            throw new BusinessException(ExceptionCode.WRONG_ADMINROLE);
        }
        Role role = roleService.selectRole(select.get(0).getRid());
        logService.insertLog("根据Id查询管理员",admin.getName());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("admin",admin);
        jsonResult.put("role",role);
        jsonResult.setMsg("根据Id查询管理员成功");
        return jsonResult.getDataMap();
    }

    @Autowired
    private AdminRoleVoService adminRoleVoService;

    @ApiOperation("编辑修改管理员")
    @PostMapping("/updateManager")
    @ApiImplicitParam(name = "adminRoleVo", value = "添加编辑账号下的所有参数", required = true)
    public Map updateManager(@RequestBody AdminRoleVo adminRoleVo) throws BusinessException, UnknownHostException {
        adminRoleVoService.updateAdmin(adminRoleVo);
        logService.insertLog("编辑修改管理员",adminRoleVo.getName());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("编辑修改管理员成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("增加管理员")
    @PostMapping("/addManager")
    @ApiImplicitParam(name = "adminRoleVo", value = "添加编辑账号下的所有参数,账号：account，姓名：name，密码：pwd，手机号：phone，电子邮件：mail，角色：desc，状态：state", required = true)
    public Map addManager(@RequestBody AdminRoleVo adminRoleVo) throws BusinessException, UnknownHostException {
        adminRoleVoService.addAdmin(adminRoleVo);
        logService.insertLog("编辑修改管理员",adminRoleVo.getName());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("增加管理员成功");
        return jsonResult.getDataMap();
    }



}
